//
//  TestTableViewCell.m
//  10-CustomTableViewCell
//
//  Created by Snow on 16/7/27.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "TestTableViewCell.h"
#import "UIView+Frame.h"
//使用@class声明的类 需要在.m中去import
#import "NewsObject.h"
@interface TestTableViewCell ()
@property (nonatomic, strong) UIImageView *imageviewOfPic;
@property (nonatomic, strong) UILabel *labelOfTitle;
@property (nonatomic, strong) UILabel *labelOfContent;
@property (nonatomic, strong) UILabel *labelOfNumber;

@end
@implementation TestTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //tableview的默认大小 宽 320 高 44
        //在初始化时 cell的frame 是初始值 不能为自定义视图布局
        NSLog(@"初始化时, cell的frame是%@",NSStringFromCGRect(self.frame));
        //添加子view
        _imageviewOfPic = [[UIImageView alloc] init];
        _labelOfTitle = [[UILabel alloc] init];
        _labelOfNumber = [[UILabel alloc] init];
        _labelOfContent = [[UILabel alloc] init];
        //初始化方法中 只负责创建
        
        //3.创建的子视图应该添加在哪个父视图
        //contentview 显示内容
        [self.contentView addSubview:_imageviewOfPic];
        [self.contentView addSubview:_labelOfTitle];
        [self.contentView addSubview:_labelOfContent];
        [_labelOfContent addSubview:_labelOfNumber];
    }
    return self;
}
//有内容/坐标改变时, 会触发该方法
- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat leftMargin = 5;
    CGFloat topMargin = 5;
    CGFloat bottomMargin = 5;
    CGFloat middleMargin = 5;
    CGFloat rightMargin = 5;
    CGFloat titleLabelHeight = 30;
    _imageviewOfPic.frame = CGRectMake(leftMargin, topMargin, self.contentView.x_width / 3, self.contentView.x_height - topMargin - bottomMargin);

    _imageviewOfPic.backgroundColor = [UIColor redColor];
    CGFloat titleLabelX = _imageviewOfPic.x_x + _imageviewOfPic.x_width + middleMargin;
    _labelOfTitle.frame = CGRectMake(titleLabelX, topMargin, self.contentView.x_width - titleLabelX - rightMargin, titleLabelHeight);
    _labelOfTitle.backgroundColor = [UIColor cyanColor];
    CGFloat contentY = topMargin +  _labelOfTitle.x_height + middleMargin;
    _labelOfContent.frame = CGRectMake(titleLabelX, topMargin + _labelOfTitle.x_height + bottomMargin, self.contentView.x_width - titleLabelX - rightMargin, self.contentView.x_height - contentY - bottomMargin);
    _labelOfContent.backgroundColor = [UIColor blueColor];
    CGFloat numberCountX = _labelOfContent.x_width * 3 / 5;
    CGFloat numberCountY = _labelOfContent.x_height * 3 / 5;
    CGFloat numberWidth = _labelOfContent.x_width * 2 / 5;
    CGFloat numberHeight = _labelOfContent.x_height / 3;
    //1.判断父视图
//    if (_labelOfNumber.superview == _labelOfContent) {
    //2.改变numbercountlabel 的父视图
    [_labelOfContent addSubview:_labelOfNumber];
        _labelOfNumber.frame = CGRectMake(numberCountX, numberCountY, numberWidth, numberHeight);
        _labelOfNumber.backgroundColor = [UIColor greenColor];
        //更换坐标系
        CGRect newrect = [_labelOfNumber.superview convertRect:_labelOfNumber.frame toView:self.contentView];
        _labelOfNumber.frame = newrect;
        [self.contentView addSubview:_labelOfNumber];
//    }
}
- (void)setNewsObject:(NewsObject *)newsObject
{
    //给自定义视图赋值
    _newsObject = newsObject;
    self.labelOfTitle.text = newsObject.title;;
    self.labelOfContent.text = newsObject.newsDescription;
    self.imageviewOfPic.image = [UIImage imageNamed:newsObject.newsImage];
        self.labelOfContent.numberOfLines = 0;
        self.labelOfContent.font = [UIFont systemFontOfSize:15];
        self.labelOfNumber.text = newsObject.newsNumberCount;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
